<?php
 /**
 *  Class CF_Request provides utilites for pulling data, specified by a parameters list array, into common data  arraystructures
 *  This allows other classes to use the data from the data arrays structures independant of the repersentation of the parameters
 *  in the Request 
 *   Data Array Structure Format (array result) is an array of arrays in all cases (even when only one variable or variable set is fetched)
 *  result[<rownumber>] [<parametername>] = <parameter value>
 * <rownumber> starts at 0, and only 0 exists for single rows
 * <parameter name> isthe name of the parameter as it is to be used in the data array.  If a prefix is specified in the fetch function, 
 * the prefix is not included in the <parameter name> 
 * <parameter value> is the parameter value
 *  
 * 
 */
class CF_Request{

	var $value_delimiter = ',';
	var $set_delimiter = ';';

	// Checked ok
	/**
	* @param array list   This is an array of parameter names for the function to get from the Request
	* @param string prefix  This prefix is added  when pulling the parameter from the request.  ex:  if value is 'thing1' and prefix is 'flt_' then the value will come from $_REQUEST['flt_thing1'] 
	* @param bool requireall (default false)  If true, then the function will return false if all parameters specified in the $list are not present.  usefull for ensuring all keys are met
	*/
		function GetReqParamsByList($list,$prefix = '',$requireall = false){
			$foundall = true;
			foreach ($list as $col_name){
				if((isset($_REQUEST[$prefix . $col_name])) AND ($_REQUEST[$prefix . $col_name] != '') ){
					$result[0][$col_name] = $_REQUEST[$prefix .$col_name];
					}else{
					   $foundall = false;
					}
				}
			if($requireall){
				if($foundall){
					return $result;
				}else{
					return false;
				}			
			}else{
				return $result;
			}
		}
		
	// Checked OK
	/**
	*
	*@param VarParam This is the name of the Request parameter  which specifies the name and order of the variables we are pulling
	*@param ValParam This is the name of the Request parameter which contains the values of the parameters
	*@param ValueDelim This is the delimiter used for seperating variable names in VarParam  and variable Values in VarParam in the request
	*/
	function GetPackedParamValues($VarParam, $ValParam, $ValueDelim = ','){
		if (isset($_REQUEST[$ValParam])) {
			$strValues = $_REQUEST[$ValParam];
			$arValues = explode($ValueDelim, $strValues);
			$proceed = true;
			}
		else {
			$proceed = false;
		}
		
		if (isset($_REQUEST[$VarParam])) {
			$strVariables = $_REQUEST[$VarParam];
			$arVariables = explode($ValueDelim, $strVariables);
		}
		else {
			$strVariables = 0;
			$proceed = false;
		}
		// Assign the value for each key variable into the $keyvalues array (position 0 since only one row specified)
		if($proceed){
			foreach($arVariables AS $index=>$keyvar){
				$keyval = $arValues[$index];
				$resultline[$keyvar] = $keyval;
				//print "KEY $keyvar = $keyval \n";
			}
			$result[0] = $resultline;
			return $result;
		}				
	}
	
	//Checked OK
	/**
	*
 	* Gets a single variable / value pair from the request
	*  Where both the variable name and value are passed in the Request as parameters
	* @param $varParam is the name of the request parameter that holds the variable name
	* @param $valParam is the name of the request parameter that holds the value
	*/
	function GetSingleValue($varParam = 'var',$valParam = 'value'){
			$changecol = $_REQUEST[$varParam];
			$changevalue = $_REQUEST[$valParam];
			$result[0][$changecol] = $changevalue;
			return $result;
	}
	
	// Checked OK
	/**
	*
	*
	*
	*
	*@param VarParam This is the name of the Request parameter  which specifies the name and order of the variables we are pulling
	*@param ValParam This is the name of the Request parameter which contains the values of the parameters
	*@param ValueDelim This is the delimiter used for seperating variable names in VarParam  and variable Values in VarParam in the request (defaults to ',')
	*@param SetDelim This is the delimiter used for seperating variable names in VarParam  and variable Values in VarParam in the request (defaults to ';')
	*/
	function GetPackedParamSets($VarParam, $SetParam, $ValueDelim = ',', $SetDelim = ';'){
		$dochange = false;
		if(!empty($_REQUEST[$SetParam]) OR $_REQUEST[$SetParam] === 0){
			// Check for the variable order, in the comma list 'VarParam'
			if (isset($_REQUEST[$VarParam])) {
				$strVariables = $_REQUEST[$VarParam];
				$arVariables = explode($ValueDelim, $strVariables);
			}
			else {
				//$strVariables = 0;
				$executequery = false;
			}	
			// produce an array ordering key variables
			foreach($arVariables AS $orderindex=>$keyvar){
				$keyorder[$orderindex] = $keyvar;
			}
			// split up sets of key values and assign to 
			$arSets = explode($SetDelim, $_REQUEST[$SetParam]);
	        foreach($arSets AS $setindex=>$arTheSet){
				$arValues = explode($ValueDelim, $arTheSet);
	            foreach($arValues AS $keyindex=>$keyvalue){
						$keyval = $keyorder[$keyindex];
						$valueset[$keyval] = $keyvalue;
	                }
				$result[] = $valueset;	
	            }
				return $result;
		} // multikey element is not empty	
	}
// Utility Functions
	// For the case where multiple lines (identified by a key set) are filled to the same newvalues
	function FillMultipleToRow($ar_row, &$ar_newvalues, $count){
		for($i = 0;$i<$count; $i++){
			$ar_newvaules[$i] = $ar_row;
		}
	}

	
		// If any of the keys on TargetList are not in $LimitList, unset them.
		function FilterByList(&$TargetList, $LimitList){
			$FlipList = array_flip($LimitList);
			foreach($TargetList AS $key => $item){
				if (!isset($FlipList[$key])){
					unset($TargetList[$key]);
				}
			}
		}
		// chck that all elements specified by $LimitList appear as keys in $TargetList
		function CheckArrayComplete($TargetList, $LimitList){
			foreach($LimitList AS $item){
				if(!isset($TargetList[$item])){
					return false;
				}
			}
			return true;
		}
	



}


?>
